package gradle.mybatis.common.config;

import gradle.mybatis.common.interceptor.SnowflakeIdInterceptor;
import org.mybatis.spring.boot.autoconfigure.MybatisAutoConfiguration;
import org.mybatis.spring.boot.autoconfigure.SqlSessionFactoryBeanCustomizer;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;

/**
 * {@link MybatisAutoConfiguration}
 */
@AutoConfiguration
@Import({SnowflakeConfig.class})
public class MybatisStarterAutoConfiguration {


    /**
     * 通过 Bean 后置处理的方式，向已存在的 SqlSessionFactoryBean 添加插件
     */
    @Bean
    @ConditionalOnBean(SnowflakeConfig.class)
    public SqlSessionFactoryBeanCustomizer sqlSessionFactoryBeanCustomizer() {
        System.setProperty("pagehelper.banner", "false");
        return sqlSessionFactoryBean -> sqlSessionFactoryBean.addPlugins(new SnowflakeIdInterceptor());
    }


}
